/**
 * 
 */
package com.vivid.dynamics.gametrials.noengine.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.vivid.dynamics.whackthemole.noengine.R;

import android.content.Context;
import android.util.Log;

/**
 * 
 * @author kusanagihk<br/>
 * created at: Nov 26, 2011::5:13:05 PM
 */
public class LogUtil {
	
	static final String logConfigFile="log_config.txt";
	
	static final String CONST_COMMENT="#";
	static final String CONST_SEPARATOR="=";
	static final String CONST_VERBOSE="verbose.enable";
	static final String CONST_DEBUG="debug.enable";
	static final String CONST_INFO="info.enable";
	static final String CONST_WARNING="warning.enable";
	static final String CONST_ERROR="error.enable";
	
	boolean verbose=false;
	boolean debug=false;
	boolean info=false;
	boolean warning=false;
	boolean error=false;
	
	protected static LogUtil instance;
	
	/**
	 * singleton method
	 * @param pContext
	 * @return
	 */
	public static LogUtil getInstance(final Context pContext) {
		if (instance==null) {
			synchronized (LogUtil.class) {
				instance=new LogUtil(pContext);
			}
		}
		return instance;
	}
	
	/**
	 * protected constructor.
	 */
	protected LogUtil(final Context pContext) {
		// protected singleton
		this.loadConfig(pContext);
	}
	
	/**
	 * method to load and parse the given config file; in case the file not 
	 * found; print out the stacktrace
	 */
	private void loadConfig(final Context pContext) {
		BufferedReader aReader=null;
		
		try {
			String aLine=null;
			
			aReader=new BufferedReader(
				new InputStreamReader(
					pContext.getResources().openRawResource(R.raw.log_config)
			));
			while ((aLine=aReader.readLine())!=null) {
				this.parseConfig(aLine);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (aReader!=null) try { aReader.close(); } catch(Exception e) {}
		}
	}
	
	/**
	 * try to parse the given line of configuration
	 * @param pLine
	 */
	private void parseConfig(final String pLine) {
		int aIdx=-1;
		
		if (!pLine.startsWith(CONST_COMMENT)) {
			aIdx=pLine.indexOf(CONST_SEPARATOR);
			
			if (pLine.startsWith(CONST_VERBOSE) && aIdx!=-1) {
				this.verbose=Boolean.parseBoolean(pLine.substring(aIdx+1));
				
			} else if (pLine.startsWith(CONST_DEBUG) && aIdx!=-1) {
				this.debug=Boolean.parseBoolean(pLine.substring(aIdx+1));
				
			} else if (pLine.startsWith(CONST_INFO) && aIdx!=-1) {
				this.info=Boolean.parseBoolean(pLine.substring(aIdx+1));
				
			} else if (pLine.startsWith(CONST_WARNING) && aIdx!=-1) {
				this.warning=Boolean.parseBoolean(pLine.substring(aIdx+1));
				
			} else if (pLine.startsWith(CONST_ERROR) && aIdx!=-1) {
				this.error=Boolean.parseBoolean(pLine.substring(aIdx+1));
				
			}
		}
	}
	
	
	public void v(final String pTag, final String pMessage) {
		if (this.verbose) {
			Log.v(pTag, pMessage);
		}
	}
	public void v(final String pTag, final String pMessage, final Throwable pThrowable) {
		if (this.verbose) {
			Log.v(pTag, pMessage, pThrowable);
		}
	}
	
	public void d(final String pTag, final String pMessage) {
		if (this.debug) {
			Log.d(pTag, pMessage);
		}
	}
	public void d(final String pTag, final String pMessage, final Throwable pThrowable) {
		if (this.debug) {
			Log.d(pTag, pMessage, pThrowable);
		}
	}
	
	public void i(final String pTag, final String pMessage) {
		if (this.info) {
			Log.i(pTag, pMessage);
		}
	}
	public void i(final String pTag, final String pMessage, final Throwable pThrowable) {
		if (this.info) {
			Log.i(pTag, pMessage, pThrowable);
		}
	}
	
	public void w(final String pTag, final String pMessage) {
		if (this.warning) {
			Log.w(pTag, pMessage);
		}
	}
	public void w(final String pTag, final String pMessage, final Throwable pThrowable) {
		if (this.warning) {
			Log.w(pTag, pMessage, pThrowable);
		}
	}
	
	public void e(final String pTag, final String pMessage) {
		if (this.error) {
			Log.e(pTag, pMessage);
		}
	}
	public void e(final String pTag, final String pMessage, final Throwable pThrowable) {
		if (this.error) {
			Log.e(pTag, pMessage, pThrowable);
		}
	}
	
}
